﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;

public partial class webUserControl_CustomerSearch : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            Cookies aCookies = new Cookies();
            int flag = int.Parse(aCookies.getCookies("cFlag"));
            int isType = int.Parse(aCookies.getCookies("cisType"));
            int districtID = int.Parse(aCookies.getCookies("cDistrictID"));
            int cityID = int.Parse(aCookies.getCookies("cCityID"));
            
            if (flag == 1)
            {
                AddItems(ddlCityID, "sysCityName", "0", 0);

                ddlDistrictID.Items.Clear();
                Query q = new Query("sysDistrictName");
                q.AddWhere("cityid", ddlCityID.SelectedValue);
                IDataReader dr = q.ExecuteReader();

                ListItem _li = new ListItem();
                _li.Text = "全部显示";
                _li.Value = "0";
                ddlDistrictID.Items.Add(_li);
                while (dr.Read())
                {
                    ListItem li = new ListItem();
                    li.Value = dr[0].ToString();
                    li.Text = dr[1].ToString();
                    ddlDistrictID.Items.Add(li);
                }
            }

            if (flag == 0)
            {
                AddItems(ddlCityID, "sysCityName", cityID.ToString(), 0);
                ddlCityID.Enabled = false;

                ddlDistrictID.Items.Clear();
                Query q = new Query("sysDistrictName");
                q.AddWhere("cityid", ddlCityID.SelectedValue);
                IDataReader dr = q.ExecuteReader();
                ListItem _li = new ListItem();
                _li.Text = "全部显示";
                _li.Value = "0";
                ddlDistrictID.Items.Add(_li);
                while (dr.Read())
                {
                    ListItem li = new ListItem();
                    li.Value = dr[0].ToString();
                    li.Text = dr[1].ToString();
                    ddlDistrictID.Items.Add(li);
                }
                
            }
 
           
        }
        Cookies a=new Cookies();
        if (a.getCookies("cisType") == "5")
        {
            Panel2.Visible = false;
            Panel3.Visible = false;
            btnExcel.Visible = false;
            Button1.Visible = false;
        }
        if(a.getCookies("cistype")=="4")
        {
            btnExcel.Visible = false;
        }
    }
    protected void ddlCityID_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlDistrictID.Items.Clear();
        ListItem _li = new ListItem("全部显示", "0");
        ddlDistrictID.Items.Add(_li);
        Query q = new Query("sysDistrictName");
        q.AddWhere("cityid", ddlCityID.SelectedValue);
        IDataReader dr = q.ExecuteReader();
        while (dr.Read())
        {
            ListItem li = new ListItem();
            li.Value = dr[0].ToString();
            li.Text = dr[1].ToString();
            ddlDistrictID.Items.Add(li);
        } 
    }
    static void AddItems(DropDownList ddl, string tb, string id, int isType)
    {
        if (isType == 1)
        {
            for (int i = 0; i <= 10; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                if (id == i.ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }
        else
        {
            Query q = new Query(tb);
            IDataReader dr = q.ExecuteReader();
            ListItem _li= new ListItem();
            _li.Text = "全部显示";
            _li.Value = "0";
            ddl.Items.Add(_li);
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                if (id == dr[0].ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StaticDataBind();
        GridView1.Columns.Clear();
        UltraWebGrid1.Columns.Clear();
        UltraWebGrid2.Columns.Clear();
        Query q=new Query("vw_supplierInfoDetail");
        Query _q = new Query("vw_supplierInfoDetail");
        Query __q = new Query("vw_OrderDetail");

            q.AddWhere("typeid", ddlisType.SelectedValue);
            _q.AddWhere("typeid", ddlisType.SelectedValue);
            __q.AddWhere("typeid", ddlisType.SelectedValue);
            if (ddlCityID.SelectedValue != "0")
            {
                q.AddWhere("cityID", ddlCityID.SelectedValue);
                _q.AddWhere("cityID", ddlCityID.SelectedValue);
                __q.AddWhere("cityID", ddlCityID.SelectedValue);

            }
            if (ddlDistrictID.SelectedValue!="0")
            {
                 q.AddWhere("districtID", ddlDistrictID.SelectedValue);
                _q.AddWhere("districtID", ddlDistrictID.SelectedValue);
                __q.AddWhere("districtID", ddlDistrictID.SelectedValue);
            }
            if (ddlCatalog.SelectedValue != "0")
            {
                q.AddWhere("catalogid", ddlCatalog.SelectedValue);
                _q.AddWhere("catalogid", ddlCatalog.SelectedValue);
                __q.AddWhere("catalogid", ddlCatalog.SelectedValue);
            }
            if (ddlDangCi.SelectedValue!="0")
            {
                q.AddWhere("dangciID", ddlDangCi.SelectedValue);
                _q.AddWhere("dangciID", ddlDangCi.SelectedValue);
                __q.AddWhere("dangciID", ddlDangCi.SelectedValue);

            }
            if(tbID.Text != "")
            {
                q.AddWhere("ID", tbID.Text);
                _q.AddWhere("ID", tbID.Text);
                __q.AddWhere("supplierID", tbID.Text);
            }
            if(tbSuppliername.Text!="")
            {
                q.AddWhere("suppliername", Comparison.Like, "%" + tbSuppliername.Text + "%");
                _q.AddWhere("suppliername", Comparison.Like, "%" + tbSuppliername.Text + "%");
                __q.AddWhere("suppliername", Comparison.Like, "%" + tbSuppliername.Text + "%");
            }
        //Response.Write(q.GetSql());
        __q.AddWhere("status", 0);
        __q.SetSelectList(
            "istype as 类型,SupplierID as 点位编号,numid as 合同号,suppliername as 点位名称, suppliername2 as 楼层,addre as 点位地址, nummale as 男,numfemale as 女, numhonggan as 烘干,numbaojian as 包间,numqita as [其他/消防框架],isType as 类型,Classname as 分类,dangciname as 档次,cityName as 城市,districtname as 分区, customerID as 客户编号, customername as 客户名,customerlinkman as 客户联络人,customertelphone as  客户电话,CustomerAddress as 客户地址");
        UltraWebGrid2.DataSource = __q.ExecuteDataSet();
        UltraWebGrid2.DataBind();
        sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings["linfen"].ConnectionString;
        sqlDS.SelectCommand = q.GetSql();
        
        GridView1.DataSource = q.ExecuteDataSet();
        GridView1.AutoGenerateColumns = false;
        GridView1.DataKeyNames = new string[] { "ID" };
        GridView1.AllowPaging = true;
        GridView1.AllowSorting = true;
        GridView1.PageSize = 10;

        BoundField bf1 = new BoundField();
        bf1.HeaderText = "编号";
        bf1.DataField = "ID";
        bf1.ReadOnly = true;
        bf1.SortExpression = "ID";
        GridView1.Columns.Add(bf1);

        HyperLinkField bf2 = new HyperLinkField();
        bf2.HeaderText = "点位名称";
        bf2.DataTextField = "SupplierName";
        Cookies c=new Cookies();
        if(c.getCookies("cisType")!="5")
        {   
            bf2.DataNavigateUrlFields = new string[] { "ID" };
            bf2.DataNavigateUrlFormatString = "~/PointsTimeUp.aspx?sID={0}";
        }
        bf2.SortExpression = "SupplierName";
        GridView1.Columns.Add(bf2);
        

        BoundField bf3 = new BoundField();
        bf3.HeaderText = "楼层";
        bf3.DataField = "SupplierName2";
        bf3.ReadOnly = true;
        bf3.SortExpression = "SupplierName2";
        GridView1.Columns.Add(bf3);

        BoundField bf10 = new BoundField();
        bf10.HeaderText = "城市";
        bf10.DataField = "cityname";
        bf10.ReadOnly = true;
        bf10.SortExpression = "cityname";
        GridView1.Columns.Add(bf10);

        BoundField bf11 = new BoundField();
        bf11.HeaderText = "分区";
        bf11.DataField = "districtname";
        bf11.ReadOnly = true;
        bf11.SortExpression = "districtname";
        GridView1.Columns.Add(bf11);

        BoundField bf12 = new BoundField();
        bf12.HeaderText = "档次";
        bf12.DataField = "dangciname";
        bf12.ReadOnly = true;
        bf12.SortExpression = "dangciname";
        GridView1.Columns.Add(bf12);

        BoundField bf13 = new BoundField();
        bf13.HeaderText = "分类";
        bf13.DataField = "ClassName";
        bf13.ReadOnly = true;
        bf13.SortExpression = "ClassName";
        GridView1.Columns.Add(bf13);

        BoundField bf14 = new BoundField();
        bf14.HeaderText = "类型";
        bf14.DataField = "isType";
        bf14.ReadOnly = true;
        bf14.SortExpression = "istype";
        GridView1.Columns.Add(bf14);

        string str = "ID as 点位编号,numiD as 合同号,SupplierName as 点位名称,supplierName2 as 楼层";
        Cookies aCookies=new Cookies();
        if(aCookies.getCookies("cisType")=="5")
        {
            foreach (ListItem li in chkDianWei.Items)
            {
                if (li.Selected)
                {
                    string s = li.Value;
                    switch (s)
                    {
                        case "NumMale":
                            BoundField bf4 = new BoundField();
                            bf4.HeaderText = "男";
                            bf4.DataField = "NumMale";
                            bf4.ReadOnly = true;
                            bf4.SortExpression = "NumMale";
                            GridView1.Columns.Add(bf4);
                            break;
                        case "NumFemale":
                            BoundField bf5 = new BoundField();
                            bf5.HeaderText = "女";
                            bf5.DataField = "NumFemale";
                            bf5.ReadOnly = true;
                            bf5.SortExpression = "NumFemale";
                            GridView1.Columns.Add(bf5);
                            break;
                        case "NumBaoJian":
                            BoundField bf6 = new BoundField();
                            bf6.HeaderText = "包间";
                            bf6.DataField = "NumBaoJian";
                            bf6.ReadOnly = true;
                            bf6.SortExpression = "NumBaoJian";
                            GridView1.Columns.Add(bf6);
                            break;
                        case "NumHongGan":
                            BoundField bf7 = new BoundField();
                            bf7.HeaderText = "烘干机";
                            bf7.DataField = "NumHongGan";
                            bf7.ReadOnly = true;
                            bf7.SortExpression = "NumHongGan";
                            GridView1.Columns.Add(bf7);
                            break;
                        case "NumQiTa":
                            BoundField bf8 = new BoundField();
                            bf8.HeaderText = "其他";
                            bf8.DataField = "NumQiTa";
                            bf8.ReadOnly = true;
                            bf8.SortExpression = "NumQiTa";
                            GridView1.Columns.Add(bf8);
                            break;
                        case "bak":
                            BoundField bf9 = new BoundField();
                            bf9.HeaderText = "备注";
                            bf9.DataField = "bak";
                            bf9.ItemStyle.Wrap = true;
                            bf9.ItemStyle.Width = Unit.Pixel(80);
                            bf9.ReadOnly = true;
                            bf9.SortExpression = "bak";
                            GridView1.Columns.Add(bf9);
                            str = str + ",bak as 备注";
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        else
        {
            foreach (ListItem li in chkDianWei.Items)
            {
                if (li.Selected)
                {
                    string s = li.Value;
                    switch (s)
                    {
                        case "NumMale":
                            BoundField bf4 = new BoundField();
                            bf4.HeaderText = "男";
                            bf4.DataField = "NumMaleDetails";
                            bf4.ReadOnly = true;
                            bf4.SortExpression = "NumMaleDetails";
                            GridView1.Columns.Add(bf4);
                            str = str + ",numMaleDetails as 男";
                            str = str + ",NumMale_O as 男总,nummale as 男空,nummale_R as 男占";

                            break;
                        case "NumFemale":
                            BoundField bf5 = new BoundField();
                            bf5.HeaderText = "女";
                            bf5.DataField = "NumFemaleDetails";
                            bf5.ReadOnly = true;
                            bf5.SortExpression = "NumFemaleDetails";
                            GridView1.Columns.Add(bf5);
                            str = str + ",NumFemaleDetails as 女";
                            str = str + ",NumfeMale_O as 女总,numfemale as 女空,numfemale_R as 女占";

                            break;
                        case "NumBaoJian":
                            BoundField bf6 = new BoundField();
                            bf6.HeaderText = "包间";
                            bf6.DataField = "NumBaoJianDetails";
                            bf6.ReadOnly = true;
                            bf6.SortExpression = "NumBaoJianDetails";
                            GridView1.Columns.Add(bf6);
                            str = str + ",NumBaoJianDetails as 包间";
                            str = str + ",Numbaojian_O as 包间总,numbaojian as 包间空,numbaojian_R as 包间占";

                            break;
                        case "NumHongGan":
                            BoundField bf7 = new BoundField();
                            bf7.HeaderText = "烘干机";
                            bf7.DataField = "NumHongGanDetails";
                            bf7.ReadOnly = true;
                            bf7.SortExpression = "NumHongGanDetails";
                            GridView1.Columns.Add(bf7);
                            str = str + ",NumHongGanDetails as 烘干机";
                            str = str + ",Numhonggan_O as 烘干总,numhonggan as 烘干空,numhonggan_R as 烘干占";

                            break;
                        case "NumQiTa":
                            BoundField bf8 = new BoundField();
                            bf8.HeaderText = "其他";
                            bf8.DataField = "NumQiTaDetails";
                            bf8.ReadOnly = true;
                            bf8.SortExpression = "NumQiTaDetails";
                            GridView1.Columns.Add(bf8);
                            str = str + ",NumQiTaDetails as 其他";
                            str = str + ",Numqita_O as 其他总,Numqita as 其他空,Numqita_R as 其他占";

                            break;
                        case "bak":
                            BoundField bf9 = new BoundField();
                            bf9.HeaderText = "备注";
                            bf9.DataField = "bak";
                            bf9.ReadOnly = true;
                            bf9.ItemStyle.Wrap = true;
                            bf9.ItemStyle.Width = Unit.Pixel(80);
                            bf9.SortExpression = "bak";
                            GridView1.Columns.Add(bf9);
                            str = str + ",bak as 备注";
                            break;
                        default:
                            break;
                    }
                }
            } 
            
        }
        str = str + ",ClassName as 分类, DangciName as 档次, cityName as 城市 ,DistrictName as 分区, LinkMan as 联络人,Telphone as 电话,Addre as 地址";



        _q.SetSelectList(str);
        UltraWebGrid1.DataSource = _q.ExecuteDataSet();
        UltraWebGrid1.DataBind();
        GridView1.DataBind();
       
        
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortColumn"] = e.SortExpression.ToString();
        if (ViewState["sortDirection"]==null)
        {
            ViewState["sortDirection"] = "ASC";
        }
        
        GridView1.Columns.Clear();
        Query q = new Query("vw_supplierInfoDetail");
        q.AddWhere("typeid", ddlisType.SelectedValue);
        if (ddlCityID.SelectedValue != "0")
        {
            q.AddWhere("cityID", ddlCityID.SelectedValue);

        }
        if (ddlDistrictID.SelectedValue != "0")
        {
            q.AddWhere("districtID", ddlDistrictID.SelectedValue);
        }
        if (ddlCatalog.SelectedValue != "0")
        {
            q.AddWhere("catalogid", ddlCatalog.SelectedValue);
        }
        if (ddlDangCi.SelectedValue != "0")
        {
            q.AddWhere("dangciID", ddlDangCi.SelectedValue);
        }
        if (tbID.Text != "")
        {
            q.AddWhere("ID", tbID.Text);
        }
        if (tbSuppliername.Text != "")
        {
            q.AddWhere("suppliername", Comparison.Like, "%" + tbSuppliername.Text + "%");
        }
        sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings["linfen"].ConnectionString;

        if ("ASC" == ViewState["sortDirection"].ToString())
        {
            ViewState["sortDirection"] = "DESC";
        }
        else
        {
            ViewState["sortDirection"] = "ASC";
        }
 
        q.ORDER_BY( e.SortExpression.ToString() + " " + ViewState["sortDirection"]);
        sqlDS.SelectCommand = q.GetSql()  ;
        GridView1.DataSource = q.ExecuteDataSet();
        GridView1.AutoGenerateColumns = false;
        GridView1.DataKeyNames = new string[] { "ID" };
        GridView1.AllowPaging = true;
        GridView1.AllowSorting = true;
        GridView1.PageSize = 10;

        BoundField bf1 = new BoundField();
        bf1.HeaderText = "编号";
        bf1.DataField = "ID";
        bf1.ReadOnly = true;
        bf1.SortExpression = "ID";
        GridView1.Columns.Add(bf1);

        HyperLinkField bf2 = new HyperLinkField();
        bf2.HeaderText = "点位名称";
        bf2.DataTextField = "SupplierName";
        Cookies c = new Cookies();
        if (c.getCookies("cisType") != "5")
        {
            bf2.DataNavigateUrlFields = new string[] { "ID" };
            bf2.DataNavigateUrlFormatString = "~/PointsTimeUp.aspx?sID={0}";
        }
        bf2.SortExpression = "SupplierName";
        GridView1.Columns.Add(bf2);

        BoundField bf3 = new BoundField();
        bf3.HeaderText = "楼层";
        bf3.DataField = "SupplierName2";
        bf3.ReadOnly = true;
        bf3.SortExpression = "SupplierName2";
        GridView1.Columns.Add(bf3);

        BoundField bf10 = new BoundField();
        bf10.HeaderText = "城市";
        bf10.DataField = "cityname";
        bf10.ReadOnly = true;
        bf10.SortExpression = "cityname";
        GridView1.Columns.Add(bf10);

        BoundField bf11 = new BoundField();
        bf11.HeaderText = "分区";
        bf11.DataField = "districtname";
        bf11.ReadOnly = true;
        bf11.SortExpression = "districtname";
        GridView1.Columns.Add(bf11);

        BoundField bf12 = new BoundField();
        bf12.HeaderText = "档次";
        bf12.DataField = "dangciname";
        bf12.ReadOnly = true;
        bf12.SortExpression = "dangciname";
        GridView1.Columns.Add(bf12);

        BoundField bf13 = new BoundField();
        bf13.HeaderText = "分类";
        bf13.DataField = "ClassName";
        bf13.ReadOnly = true;
        bf13.SortExpression = "ClassName";
        GridView1.Columns.Add(bf13);

        BoundField bf14 = new BoundField();
        bf14.HeaderText = "类型";
        bf14.DataField = "isType";
        bf14.ReadOnly = true;
        bf14.SortExpression = "istype";
        GridView1.Columns.Add(bf14);

        string str = "SupplierName as 点位名称,supplierName2 as 楼层";
        Cookies aCookies = new Cookies();
        if (aCookies.getCookies("cisType") == "5")
        {
            foreach (ListItem li in chkDianWei.Items)
            {
                if (li.Selected)
                {
                    string s = li.Value;
                    switch (s)
                    {
                        case "NumMale":
                            BoundField bf4 = new BoundField();
                            bf4.HeaderText = "男";
                            bf4.DataField = "NumMale";
                            bf4.ReadOnly = true;
                            bf4.SortExpression = "NumMale";
                            GridView1.Columns.Add(bf4);
                            break;
                        case "NumFemale":
                            BoundField bf5 = new BoundField();
                            bf5.HeaderText = "女";
                            bf5.DataField = "NumFemale";
                            bf5.ReadOnly = true;
                            bf5.SortExpression = "NumFemale";
                            GridView1.Columns.Add(bf5);
                            break;
                        case "NumBaoJian":
                            BoundField bf6 = new BoundField();
                            bf6.HeaderText = "包间";
                            bf6.DataField = "NumBaoJian";
                            bf6.ReadOnly = true;
                            bf6.SortExpression = "NumBaoJian";
                            GridView1.Columns.Add(bf6);
                            break;
                        case "NumHongGan":
                            BoundField bf7 = new BoundField();
                            bf7.HeaderText = "烘干机";
                            bf7.DataField = "NumHongGan";
                            bf7.ReadOnly = true;
                            bf7.SortExpression = "NumHongGan";
                            GridView1.Columns.Add(bf7);
                            break;
                        case "NumQiTa":
                            BoundField bf8 = new BoundField();
                            bf8.HeaderText = "其他";
                            bf8.DataField = "NumQiTa";
                            bf8.ReadOnly = true;
                            bf8.SortExpression = "NumQiTa";
                            GridView1.Columns.Add(bf8);
                            break;
                        case "bak":
                            BoundField bf9 = new BoundField();
                            bf9.HeaderText = "备注";
                            bf9.DataField = "bak";
                            bf9.ReadOnly = true;
                            bf9.ItemStyle.Wrap = true;
                            bf9.ItemStyle.Width = Unit.Pixel(80);
                            bf9.SortExpression = "bak";
                            GridView1.Columns.Add(bf9);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        else
        {
            foreach (ListItem li in chkDianWei.Items)
            {
                if (li.Selected)
                {
                    string s = li.Value;
                    switch (s)
                    {
                        case "NumMale":
                            BoundField bf4 = new BoundField();
                            bf4.HeaderText = "男";
                            bf4.DataField = "NumMaleDetails";
                            bf4.ReadOnly = true;
                            bf4.SortExpression = "NumMaleDetails";
                            GridView1.Columns.Add(bf4);
                            str = str + ",numMaleDetails as 男";
                            break;
                        case "NumFemale":
                            BoundField bf5 = new BoundField();
                            bf5.HeaderText = "女";
                            bf5.DataField = "NumFemaleDetails";
                            bf5.ReadOnly = true;
                            bf5.SortExpression = "NumFemaleDetails";
                            GridView1.Columns.Add(bf5);
                            str = str + ",NumFemaleDetails as 女";
                            break;
                        case "NumBaoJian":
                            BoundField bf6 = new BoundField();
                            bf6.HeaderText = "包间";
                            bf6.DataField = "NumBaoJianDetails";
                            bf6.ReadOnly = true;
                            bf6.SortExpression = "NumBaoJianDetails";
                            GridView1.Columns.Add(bf6);
                            str = str + ",NumBaoJianDetails as 包间";
                            break;
                        case "NumHongGan":
                            BoundField bf7 = new BoundField();
                            bf7.HeaderText = "烘干机";
                            bf7.DataField = "NumHongGanDetails";
                            bf7.ReadOnly = true;
                            bf7.SortExpression = "NumHongGanDetails";
                            GridView1.Columns.Add(bf7);
                            str = str + ",NumHongGanDetails as 烘干机";
                            break;
                        case "NumQiTa":
                            BoundField bf8 = new BoundField();
                            bf8.HeaderText = "其他";
                            bf8.DataField = "NumQiTaDetails";
                            bf8.ReadOnly = true;
                            bf8.SortExpression = "NumQiTaDetails";
                            GridView1.Columns.Add(bf8);
                            str = str + ",NumQiTaDetails as 其他";
                            break;
                        case "bak":
                            BoundField bf9 = new BoundField();
                            bf9.HeaderText = "备注";
                            bf9.DataField = "bak";
                            bf9.ReadOnly = true;
                            bf9.ItemStyle.Wrap = true;
                            bf9.ItemStyle.Width = Unit.Pixel(80);
                            bf9.SortExpression = "bak";
                            GridView1.Columns.Add(bf9);
                            str = str + ",bak as 备注";
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        
        if (ViewState["sortColumn"]==null)
        {
            ViewState["sortColumn"] = "ID";
        }
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = "ASC";
        }
        GridView1.Columns.Clear();
        Query q = new Query("vw_supplierInfoDetail");
        q.AddWhere("typeid", ddlisType.SelectedValue);
        if (ddlCityID.SelectedValue != "0")
        {
            q.AddWhere("cityID", ddlCityID.SelectedValue);

        }
        if (ddlDistrictID.SelectedValue != "0")
        {
            q.AddWhere("districtID", ddlDistrictID.SelectedValue);
        }
        if (ddlCatalog.SelectedValue != "0")
        {
            q.AddWhere("catalogid", ddlCatalog.SelectedValue);
        }
        if (ddlDangCi.SelectedValue != "0")
        {
            q.AddWhere("dangciID", ddlDangCi.SelectedValue);
        }
        if (tbID.Text != "")
        {
            q.AddWhere("ID", tbID.Text);
        }
        if (tbSuppliername.Text != "")
        {
            q.AddWhere("suppliername", Comparison.Like, "%" + tbSuppliername.Text + "%");
        }
        sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings["linfen"].ConnectionString;
        
        q.ORDER_BY(ViewState["sortColumn"] + " " + ViewState["sortDirection"]);
        sqlDS.SelectCommand = q.GetSql();
        GridView1.DataSource = q.ExecuteDataSet();
        GridView1.AutoGenerateColumns = false;
        GridView1.DataKeyNames = new string[] { "ID" };
        GridView1.AllowPaging = true;
        GridView1.AllowSorting = true;
        GridView1.PageSize = 10;

        BoundField bf1 = new BoundField();
        bf1.HeaderText = "编号";
        bf1.DataField = "ID";
        bf1.ReadOnly = true;
        bf1.SortExpression = "ID";
        GridView1.Columns.Add(bf1);

        HyperLinkField bf2 = new HyperLinkField();
        bf2.HeaderText = "点位名称";
        bf2.DataTextField = "SupplierName";
        Cookies c = new Cookies();
        if (c.getCookies("cisType") != "5")
        {
            bf2.DataNavigateUrlFields = new string[] { "ID" };
            bf2.DataNavigateUrlFormatString = "~/PointsTimeUp.aspx?sID={0}";
        }
        bf2.SortExpression = "SupplierName";
        GridView1.Columns.Add(bf2);

        BoundField bf3 = new BoundField();
        bf3.HeaderText = "楼层";
        bf3.DataField = "SupplierName2";
        bf3.ReadOnly = true;
        bf3.SortExpression = "SupplierName2";
        GridView1.Columns.Add(bf3);

        BoundField bf10 = new BoundField();
        bf10.HeaderText = "城市";
        bf10.DataField = "cityname";
        bf10.ReadOnly = true;
        bf10.SortExpression = "cityname";
        GridView1.Columns.Add(bf10);

        BoundField bf11 = new BoundField();
        bf11.HeaderText = "分区";
        bf11.DataField = "districtname";
        bf11.ReadOnly = true;
        bf11.SortExpression = "districtname";
        GridView1.Columns.Add(bf11);

        BoundField bf12 = new BoundField();
        bf12.HeaderText = "档次";
        bf12.DataField = "dangciname";
        bf12.ReadOnly = true;
        bf12.SortExpression = "dangciname";
        GridView1.Columns.Add(bf12);

        BoundField bf13 = new BoundField();
        bf13.HeaderText = "分类";
        bf13.DataField = "ClassName";
        bf13.ReadOnly = true;
        bf13.SortExpression = "ClassName";
        GridView1.Columns.Add(bf13);

        BoundField bf14 = new BoundField();
        bf14.HeaderText = "类型";
        bf14.DataField = "isType";
        bf14.ReadOnly = true;
        bf14.SortExpression = "istype";
        GridView1.Columns.Add(bf14);

        string str = "SupplierName as 点位名称,supplierName2 as 楼层";
        Cookies aCookies = new Cookies();
        if (aCookies.getCookies("cisType") == "5")
        {
            foreach (ListItem li in chkDianWei.Items)
            {
                if (li.Selected)
                {
                    string s = li.Value;
                    switch (s)
                    {
                        case "NumMale":
                            BoundField bf4 = new BoundField();
                            bf4.HeaderText = "男";
                            bf4.DataField = "NumMale";
                            bf4.ReadOnly = true;
                            bf4.SortExpression = "NumMale";
                            GridView1.Columns.Add(bf4);
                            break;
                        case "NumFemale":
                            BoundField bf5 = new BoundField();
                            bf5.HeaderText = "女";
                            bf5.DataField = "NumFemale";
                            bf5.ReadOnly = true;
                            bf5.SortExpression = "NumFemale";
                            GridView1.Columns.Add(bf5);
                            break;
                        case "NumBaoJian":
                            BoundField bf6 = new BoundField();
                            bf6.HeaderText = "包间";
                            bf6.DataField = "NumBaoJian";
                            bf6.ReadOnly = true;
                            bf6.SortExpression = "NumBaoJian";
                            GridView1.Columns.Add(bf6);
                            break;
                        case "NumHongGan":
                            BoundField bf7 = new BoundField();
                            bf7.HeaderText = "烘干机";
                            bf7.DataField = "NumHongGan";
                            bf7.ReadOnly = true;
                            bf7.SortExpression = "NumHongGan";
                            GridView1.Columns.Add(bf7);
                            break;
                        case "NumQiTa":
                            BoundField bf8 = new BoundField();
                            bf8.HeaderText = "其他";
                            bf8.DataField = "NumQiTa";
                            bf8.ReadOnly = true;
                            bf8.SortExpression = "NumQiTa";
                            GridView1.Columns.Add(bf8);
                            break;
                        case "bak":
                            BoundField bf9 = new BoundField();
                            bf9.HeaderText = "备注";
                            bf9.DataField = "bak";
                            bf9.ReadOnly = true;
                            bf9.ItemStyle.Wrap = true;
                            bf9.ItemStyle.Width = Unit.Pixel(80);
                            bf9.SortExpression = "bak";
                            GridView1.Columns.Add(bf9);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        else
        {
            foreach (ListItem li in chkDianWei.Items)
            {
                if (li.Selected)
                {
                    string s = li.Value;
                    switch (s)
                    {
                        case "NumMale":
                            BoundField bf4 = new BoundField();
                            bf4.HeaderText = "男";
                            bf4.DataField = "NumMaleDetails";
                            bf4.ReadOnly = true;
                            bf4.SortExpression = "NumMaleDetails";
                            GridView1.Columns.Add(bf4);
                            str = str + ",numMaleDetails as 男";
                            break;
                        case "NumFemale":
                            BoundField bf5 = new BoundField();
                            bf5.HeaderText = "女";
                            bf5.DataField = "NumFemaleDetails";
                            bf5.ReadOnly = true;
                            bf5.SortExpression = "NumFemaleDetails";
                            GridView1.Columns.Add(bf5);
                            str = str + ",NumFemaleDetails as 女";
                            break;
                        case "NumBaoJian":
                            BoundField bf6 = new BoundField();
                            bf6.HeaderText = "包间";
                            bf6.DataField = "NumBaoJianDetails";
                            bf6.ReadOnly = true;
                            bf6.SortExpression = "NumBaoJianDetails";
                            GridView1.Columns.Add(bf6);
                            str = str + ",NumBaoJianDetails as 包间";
                            break;
                        case "NumHongGan":
                            BoundField bf7 = new BoundField();
                            bf7.HeaderText = "烘干机";
                            bf7.DataField = "NumHongGanDetails";
                            bf7.ReadOnly = true;
                            bf7.SortExpression = "NumHongGanDetails";
                            GridView1.Columns.Add(bf7);
                            str = str + ",NumHongGanDetails as 烘干机";
                            break;
                        case "NumQiTa":
                            BoundField bf8 = new BoundField();
                            bf8.HeaderText = "其他";
                            bf8.DataField = "NumQiTaDetails";
                            bf8.ReadOnly = true;
                            bf8.SortExpression = "NumQiTaDetails";
                            GridView1.Columns.Add(bf8);
                            str = str + ",NumQiTaDetails as 其他";
                            break;
                        case "bak":
                            BoundField bf9 = new BoundField();
                            bf9.HeaderText = "备注";
                            bf9.DataField = "bak";
                            bf9.ReadOnly = true;
                            bf9.SortExpression = "bak";
                            bf9.ItemStyle.Wrap = true;
                            bf9.ItemStyle.Width = Unit.Pixel(80);
                            GridView1.Columns.Add(bf9);
                            str = str + ",bak as 备注";
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        
        
        int startIndex;
        startIndex = GridView1.PageIndex * GridView1.PageSize;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        UltraWebGridExcelExporter1.Export(UltraWebGrid1);
    }
    void StaticDataBind()
    {
        Query q = new Query("supplierinfo");
        q.AddWhere("typeid", ddlisType.SelectedValue);
        if (ddlCityID.SelectedValue != "0")
        {
            q.AddWhere("cityID", ddlCityID.SelectedValue);
        }
        if (ddlDistrictID.SelectedValue != "0")
        {
            q.AddWhere("districtID", ddlDistrictID.SelectedValue);
        }
        if (ddlCatalog.SelectedValue != "0")
        {
            q.AddWhere("catalogid", ddlCatalog.SelectedValue);
        }
        if (ddlDangCi.SelectedValue != "0")
        {
            q.AddWhere("dangciID", ddlDangCi.SelectedValue);
        }

        Query _q = new Query("supplierinfo");
        _q.AddWhere("typeid", ddlisType.SelectedValue);
        if (ddlCityID.SelectedValue != "0")
        {
            _q.AddWhere("cityID", ddlCityID.SelectedValue);
        }
        if (ddlDistrictID.SelectedValue != "0")
        {
            _q.AddWhere("districtID", ddlDistrictID.SelectedValue);
        }
        if (ddlCatalog.SelectedValue != "0")
        {
            _q.AddWhere("catalogid", ddlCatalog.SelectedValue);
        }
        if (ddlDangCi.SelectedValue != "0")
        {
            _q.AddWhere("dangciID", ddlDangCi.SelectedValue);
        }
        _q.SetSelectList("suppliername");
        _q.DISTINCT();
        if (q.GetRecordCount() > 0)
        {
            Cookies a = new Cookies();
            if (a.getCookies("cisType") == "5")
            {
                Panel2.Visible = false;
                Panel3.Visible = false;
            }
            int numMale = int.Parse(q.GetSum("nummale").ToString());
            int _numMale = int.Parse(q.GetSum("numMale_O").ToString());
            int numFemale = int.Parse(q.GetSum("numFemale").ToString());
            int _numFemale = int.Parse(q.GetSum("numFemale_O").ToString());
            int numHongGan = int.Parse(q.GetSum("numHongGan").ToString());
            int _numHongGan = int.Parse(q.GetSum("numHongGan_O").ToString());
            int numBaojian = int.Parse(q.GetSum("numBaoJian").ToString());
            int _numBaojian = int.Parse(q.GetSum("numBaoJian_O").ToString());
            int numQiTa = int.Parse(q.GetSum("numQita").ToString());
            int _numQiTa = int.Parse(q.GetSum("numQita_O").ToString());

            lblMale2.Text = numMale.ToString();
            lblFemale2.Text = numFemale.ToString();
            lblHongGan2.Text = numHongGan.ToString();
            lblBaoJian2.Text = numBaojian.ToString();
            lblQiTa2.Text = numQiTa.ToString();
            lblTotal2.Text = (numMale + numFemale + numHongGan + numBaojian + numQiTa).ToString();

            lblMale3.Text = _numMale.ToString();
            lblFemale3.Text = _numFemale.ToString();
            lblHongGan3.Text = _numHongGan.ToString();
            lblBaoJian3.Text = _numBaojian.ToString();
            lblQiTa3.Text = _numQiTa.ToString();
            lblTotal3.Text = (_numMale + _numFemale + _numHongGan + _numBaojian + _numQiTa).ToString();

            lblMale1.Text = (_numMale - numMale).ToString();
            lblFemale1.Text = (_numFemale - numFemale).ToString();
            lblHongGan1.Text = (_numHongGan - numHongGan).ToString();
            lblBaoJian1.Text = (_numBaojian - numBaojian).ToString();
            lblQiTa1.Text = (_numQiTa - numQiTa).ToString();
            lblTotal1.Text = ((_numMale + _numFemale + _numHongGan + _numBaojian + _numQiTa) - (numMale + numFemale + numHongGan + numBaojian + numQiTa)).ToString();

            lblMale4.Text = (float.Parse(numMale.ToString()) / float.Parse(_numMale.ToString())).ToString("P") == "非数字" ? "100%" : (float.Parse(numMale.ToString()) / float.Parse(_numMale.ToString())).ToString("P");
            lblFemale4.Text = (float.Parse(numFemale.ToString()) / float.Parse(_numFemale.ToString())).ToString("P") == "非数字" ? "100%" : (float.Parse(numFemale.ToString()) / float.Parse(_numFemale.ToString())).ToString("P");
            lblHongGan4.Text = (float.Parse(numHongGan.ToString()) / float.Parse(_numHongGan.ToString())).ToString("P") == "非数字" ? "100%" : (float.Parse(numHongGan.ToString()) / float.Parse(_numHongGan.ToString())).ToString("P");
            lblBaoJian4.Text = (float.Parse(numBaojian.ToString()) / float.Parse(_numBaojian.ToString())).ToString("P") == "非数字" ? "100%" : (float.Parse(numBaojian.ToString()) / float.Parse(_numBaojian.ToString())).ToString("P");
            lblQiTa4.Text = (float.Parse(numQiTa.ToString()) / float.Parse(_numQiTa.ToString())).ToString("P") == "非数字" ? "100%" : (float.Parse(numQiTa.ToString()) / float.Parse(_numQiTa.ToString())).ToString("P");
            lblTotal4.Text =
                (float.Parse((numMale + numFemale + numHongGan + numBaojian + numQiTa).ToString()) /
                 float.Parse((_numMale + _numFemale + _numHongGan + _numBaojian + _numQiTa).ToString())).ToString("P") == "非数字" ? "100%" : (float.Parse((numMale + numFemale + numHongGan + numBaojian + numQiTa).ToString()) /
                 float.Parse((_numMale + _numFemale + _numHongGan + _numBaojian + _numQiTa).ToString())).ToString("P");
            Panel1.Visible = true;
            lblSearchResult.Text = "搜索结果：" + _q.GetRecordCount() + "个商家";
        }
        else
        {
            Panel1.Visible = false;
            lblSearchResult.Text = "";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UltraWebGridExcelExporter1.Export(UltraWebGrid2);
    }
}
